package com.frog.framework.rekognition;

import com.frog.framework.rekognition.factory.RekognitionEndpointFactory;
import com.frog.framework.rekognition.utils.RekognitionUtils;
import org.springframework.util.Assert;
import software.amazon.awssdk.services.rekognition.model.*;
import software.amazon.awssdk.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuqiong 2022/11/7
 * @description
 */
public class FrogRekognitionClient {


    public static DetectModerationLabelsResponse detectModerationLabels(String clientName, String bucket, String name) {
        Assert.hasText(bucket, "参数 bucket 值不能为空,请检查");
        Assert.hasText(name, "参数 name 值不能为空,请检查");
        try {
            Image myImage = Image.builder()
                    .s3Object(S3Object.builder()
                            .bucket(bucket)
                            .name(name)
                            .build())
                    .build();
            DetectModerationLabelsRequest moderationLabelsRequest = DetectModerationLabelsRequest.builder()
                    .image(myImage)
                    .minConfidence(60F)
                    .build();
            DetectModerationLabelsResponse moderationLabelsResponse = RekognitionEndpointFactory.getRekognitionClient(clientName).detectModerationLabels(moderationLabelsRequest);
            return moderationLabelsResponse;
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }



    public static StartContentModerationResponse getStartContentModeration(
            String clientName, String bucket, String name, String roleArn, String snsTopicArn) {
        Assert.hasText(bucket, "参数 bucket 值不能为空,请检查");
        Assert.hasText(name, "参数 name 值不能为空,请检查");
        Assert.isTrue(RekognitionUtils.checkTopicArn(roleArn), "参数 roleArn 值不正确,请检查");
        Assert.isTrue(RekognitionUtils.checkTopicArn(snsTopicArn), "参数 snsTopicArn 值不正确,请检查");
        try {
            NotificationChannel channel = NotificationChannel.builder()
                    .roleArn(roleArn)
                    .snsTopicArn(snsTopicArn)
                    .build();

            Video video = Video.builder()
                    .s3Object(S3Object.builder()
                            .bucket(bucket)
                            .name(name)
                            .build())
                    .build();
            StartContentModerationRequest modDetectionRequest = StartContentModerationRequest.builder()
                    .notificationChannel(channel)
                    .video(video)
                    .build();
            StartContentModerationResponse startContentModerationResponse = RekognitionEndpointFactory.getRekognitionClient(clientName).startContentModeration(modDetectionRequest);
            return startContentModerationResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对象内 moderationLabels 是全部的 ，这个需要注意
     *
     * @param clientName
     * @param jobId
     * @return
     */
    public static GetContentModerationResponse getContentModerationResponse(String clientName, String jobId) {
        Assert.hasText(jobId, "参数 jobId 值不能为空,请检查");
        String nextToken = null;
        GetContentModerationResponse resultContentModerationResponse = null;
        List<ContentModerationDetection> contentModerationDetections = new ArrayList<>();
        try {
            do {
                GetContentModerationResponse contentModerationResponse = RekognitionEndpointFactory.getRekognitionClient(clientName).getContentModeration(GetContentModerationRequest.builder()
                        .jobId(jobId)
                        .nextToken(nextToken)
                        .sortBy(ContentModerationSortBy.TIMESTAMP)
                        .maxResults(10)
                        .build());
                if (contentModerationResponse == null) break;
                if (StringUtils.isBlank(nextToken)) {
                    resultContentModerationResponse = contentModerationResponse.toBuilder().build();
                }
                nextToken = contentModerationResponse.nextToken();
                contentModerationDetections.addAll(contentModerationResponse.moderationLabels());
            } while (StringUtils.isNotBlank(nextToken));
            resultContentModerationResponse = resultContentModerationResponse.toBuilder().moderationLabels(contentModerationDetections).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultContentModerationResponse;
    }

}
